package org.esprit.ocm.client.ui.common;

import org.esprit.ocm.client.rpc.UserServiceAsync.Util;
import org.esprit.ocm.dto.impl.UserDto;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.events.CloseClientEvent;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class Login extends Window {

	public Login() {

		setWidth(300);
		setHeight(150);
		setShowMinimizeButton(false);
		setIsModal(true);
		setShowModalMask(true);
		setShowCloseButton(false);
		centerInPage();
		addCloseClickHandler(new CloseClickHandler() {
			public void onCloseClick(CloseClientEvent event) {
				destroy();
			}
		});

		VLayout loginLayout = new VLayout();
		loginLayout.setWidth(300);
		loginLayout.setHeight(150);

		final DynamicForm form1 = new DynamicForm();
		form1.setPadding(25);
		form1.setLayoutAlign(VerticalAlignment.TOP);

		final TextItem username = new TextItem("username");
		username.setTitle("Username");
		username.setRequired(true);

		final PasswordItem password = new PasswordItem("password");
		password.setTitle("Password");
		password.setRequired(true);

		HLayout l1 = new HLayout();
		l1.setHeight("70%");
		l1.addChild(form1);
		form1.setFields(username, password);
		form1.redraw();

		IButton loginB = new IButton("Login");
		loginB.setWidth(120);
		loginB.setShowRollOver(true);
		loginB.setShowDisabled(true);
		loginB.setShowDown(true);
		loginB.setIcon("login.png");
		loginB.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {

				Util.getInstance().authenticate(username.getValue().toString(), password.getValue().toString(), new AsyncCallback<UserDto>() {

					public void onSuccess(UserDto u) {
						// TODO Auto-generated method stub
						System.out.println(username.getValue().toString() + "-" + password.getValue().toString());
						if (u != null) {

							System.out.println(u.getIdUser());
							destroy();
							ServerWindow serverWindow = new ServerWindow(u);
							serverWindow.show();
						} else {
							SC.warn("login error");
							username.clearValue();
							password.clearValue();
						}
					}

					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						System.out.println(arg0);
					}
				});
			}
		});

		IButton accountB = new IButton("Create account");
		accountB.setWidth(120);
		accountB.setShowRollOver(true);
		accountB.setShowDisabled(true);
		accountB.setShowDown(true);
		accountB.setIcon("account.png");
		accountB.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				UserDto user = new UserDto();
				user.setLogin(username.getValue().toString());
				user.setPass(password.getValue().toString());
				
				Util.getInstance().add(user, new AsyncCallback<UserDto>() {

					public void onSuccess(UserDto u) {
						System.out.println(username.getValue().toString() + "-" + password.getValue().toString());
						System.out.println(u.getIdUser());
						SC.say("Profile created successfullly");
					}

					public void onFailure(Throwable arg0) {
						// TODO Auto-generated method stub
						System.out.println(arg0);
					}
				});
			}
		});

		HLayout l2 = new HLayout();
		l2.setHeight("30%");
		l2.setPadding(5);

		VLayout log = new VLayout();
		log.setWidth("50%");
		log.addMember(loginB);
		VLayout acc = new VLayout();
		acc.setWidth("50%");
		acc.addMember(accountB);

		l2.addMember(log);
		l2.addMember(acc);

		loginLayout.addMember(l1);
		loginLayout.addMember(l2);
		addMember(loginLayout);
	}
}
